package com.linkcard.user.action;

import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.linkcard.commen.tools.Helper;
import com.linkcard.commen.tools.Page;
import com.linkcard.user.manager.WebsiteConfigerManager;
import com.linkcard.user.model.Ad;
import com.linkcard.user.model.WebsiteColumns;
import com.linkcard.user.model.WebsiteConfiger;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class WebsiteConfigerAction extends ActionSupport {

	private WebsiteConfigerManager websiteConfigerManager=null;
	private WebsiteColumns websiteColumns=null;
	private int page;
	private int rows;
	private WebsiteConfiger websiteConfiger=null;
	private Ad ad=null;
	private List adList=null;
	@Override
	public String execute() throws Exception {
		return "success";
	}
	//获得网站配置信息
	public String websiteConfig()throws Exception{
		String path = ServletActionContext.getRequest().getRealPath("/WEB-INF/classes/websiteconfig.xml");
		websiteConfiger=websiteConfigerManager.websiteConfig(path);
		return "WebsiteConfiger";
	}
	//配置网站信息
	public String configWebsite()throws Exception{
		String path = ServletActionContext.getRequest().getRealPath("/WEB-INF/classes/websiteconfig.xml");
		boolean isSuccess=this.websiteConfigerManager.configWebsite(websiteConfiger,path);
		settingWebsiteInfo();
		return "WebsiteConfiger";
	}
	
	//添加网站栏目信息
	public  String addWebsiteColumn() throws Exception
	{
		websiteColumns.setName(Helper.decode(websiteColumns.getName()));
		boolean isSuccess=websiteConfigerManager.addWebsiteColumn(websiteColumns);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		settingWebsiteInfo();
		backText(json.toString());
		return "Data";
	}
	//查找栏目
	public String findWebsiteColumn() throws Exception{
		websiteColumns=this.websiteConfigerManager.findWebsiteColumn(websiteColumns.getId());
		JSONObject json=JSONObject.fromObject(websiteColumns);
		backText(json.toString());
		return "Data";
	}
	//修改网站栏目信息
	public  String modifyWebsiteColumn() throws Exception
	{
		websiteColumns.setName(Helper.decode(websiteColumns.getName()));
		boolean isSuccess=websiteConfigerManager.modifyWebsiteColumn(websiteColumns);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		settingWebsiteInfo();
		backText(json.toString());
		return "Data";
	}
	//添加网站栏目信息
	@SuppressWarnings("unchecked")
	public  String websiteColumnList() throws Exception
	{
		Page pageObj=new Page();
		     pageObj.setCurrentPage(page);
		     pageObj.setPageSize(rows);
		List list=websiteConfigerManager.websiteColumnList(pageObj);
		JSONObject json=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			WebsiteColumns  websiteColumns=(WebsiteColumns)list.get(i);
			JSONObject jsontemp = new JSONObject();
			jsontemp.put("id",websiteColumns.getId());
			jsontemp.put("name", websiteColumns.getName());
			jsontemp.put("url",websiteColumns.getUrl());
			jsontemp.put("isNav",((websiteColumns.getIsNav()==0)?"否":"是"));
			jsontemp.put("isList",websiteColumns.getIsList());
			jsontemp.put("sort",websiteColumns.getSort());
			jsontemp.put("opt", "<a href='javascript:modify("+websiteColumns.getId()+")'>修改</>&nbsp;&nbsp;<a href='javascript:deleteColumns("+websiteColumns.getId()+")'>删除</>");
			jsonArray.add(jsontemp);
		}
		json.put("total",pageObj.getRowCount());
		json.put("rows",jsonArray);
		backText(json.toString());
		
		return "Data";
	}
	//添加广告图片
	public String adAd()throws Exception{
		String path = ServletActionContext.getRequest().getRealPath("/WEB-INF/classes/ad.xml");
		ad.setTitle(Helper.decode(ad.getTitle()));
		boolean isSuccess=websiteConfigerManager.adAd(ad,path);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		settingWebsiteInfo();
		backText(json.toString());
		return "Data";
	}
	//广告图片
	public String adList()throws Exception{
		String path = ServletActionContext.getRequest().getRealPath("/WEB-INF/classes/ad.xml");
		adList=websiteConfigerManager.adList(path);
		return "AdList";
	}
	//删除广告
	public String deleteAd() throws Exception{
		String path = ServletActionContext.getRequest().getRealPath("/WEB-INF/classes/ad.xml");
		boolean isSuccess=websiteConfigerManager.deleteAd(ad.getId(),path);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		return "Data";
	}
	//修改广告
	public String modifyAd() throws Exception{
		ad.setTitle(Helper.decode(ad.getTitle()));
		String path = ServletActionContext.getRequest().getRealPath("/WEB-INF/classes/ad.xml");
		boolean isSuccess=websiteConfigerManager.modifyAd(ad,path);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		return "Data";
	}
	//删除栏目
	public String deleteColumns() throws Exception{
		boolean isSuccess=this.websiteConfigerManager.deleteColumns(websiteColumns.getId());
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		settingWebsiteInfo();
		return "Data";
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	/**
	 * 将网站修改的内容设置到application
	 * @return
	 */
	public void settingWebsiteInfo()
	{
		List list=websiteConfigerManager.websiteColumnList(null);
		ActionContext.getContext().getApplication().put("columns", list);
		String path = ServletActionContext.getRequest().getRealPath("/WEB-INF/classes/websiteconfig.xml");
		websiteConfiger=websiteConfigerManager.websiteConfig(path);
		ActionContext.getContext().getApplication().put("website", websiteConfiger);
	}
	public WebsiteConfigerManager getWebsiteConfigerManager() {
		return websiteConfigerManager;
	}

	public void setWebsiteConfigerManager(
			WebsiteConfigerManager websiteConfigerManager) {
		this.websiteConfigerManager = websiteConfigerManager;
	}


	public WebsiteColumns getWebsiteColumns() {
		return websiteColumns;
	}

	public void setWebsiteColumns(WebsiteColumns websiteColumns) {
		this.websiteColumns = websiteColumns;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	public WebsiteConfiger getWebsiteConfiger() {
		return websiteConfiger;
	}
	public void setWebsiteConfiger(WebsiteConfiger websiteConfiger) {
		this.websiteConfiger = websiteConfiger;
	}
	public Ad getAd() {
		return ad;
	}
	public void setAd(Ad ad) {
		this.ad = ad;
	}
	public List getAdList() {
		return adList;
	}
	public void setAdList(List adList) {
		this.adList = adList;
	}
}
